package dj.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.tools.Constants;
import dj.entity.BackendUser;
import dj.service.BackendUserService;





@Controller
@RequestMapping(value="/manager")
public class UserLoginController {
	private Logger logger = Logger.getLogger(UserLoginController.class);
	
	@Resource
	private BackendUserService backendUserService;
	
	@RequestMapping(value="/login")
	public String login(){
		logger.debug("LoginController welcome AppInfoSystem backend==================");
		System.out.println(11);
		return "backendlogin"; 
	}
	
	@RequestMapping(value="/dologin",method=RequestMethod.POST)
	public String dologin(@RequestParam String userCode,@RequestParam String userPassword,HttpServletRequest request,HttpSession session){
		 BackendUser  user=backendUserService.dologin(userCode, userPassword);
		 if(user!=null){
			 session.setAttribute(Constants.USER_SESSION, user);
			 return "redirect:/manager/backend/main";
		 }else{
			 request.setAttribute("error", "用户名或密码不正确");
			return "backendlogin";
		 }
		 
	}
	
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		session.removeAttribute(Constants.USER_SESSION);
		return "backendlogin";
	}
	
	@RequestMapping(value="/backend/main")
	public String main(HttpSession session){
		BackendUser  user=(BackendUser) session.getAttribute(Constants.USER_SESSION);
		if(user==null){
			return "/manager/login";
		}
		return "/backend/main";
	}

}
